package com.czp.menu.mapper.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;

import com.czp.framework.mapper.BaseMapperSupport;
import com.czp.menu.mapper.IMenuMapper;
import com.czp.menu.model.Icon;
import com.czp.menu.model.Menu;

public class MenuMapperImpl extends BaseMapperSupport implements IMenuMapper {

	public List<Menu> getMenuList(String personId) throws Exception {
		if(personId.equals("0")) {//如果是超级管理员则查询全部
			return this.getSqlSession().selectList(
					"com.czp.menu.mapper.IMenuMapper.getManageMenuList");
		}
		return this.getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.getMenuList", personId);
	}
	
	public List<Menu> getSettingsListByPid(String pid) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.getSettingsListByPid", pid);
	}

	public List<Menu> getMenuTreeByPid(Menu menu) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.getMenuTreeByPid", menu);
	}

	public int addMenuNode(Menu menu) throws Exception {
		if(menu.getEnableStatus().equals("0")) {
			//当节点禁用时将颜色变为红色
			menu.setCls("c-tree-node-red");
		}
		return this.getSqlSession().insert(//添加节点数据
				"com.czp.menu.mapper.IMenuMapper.addMenuNode", menu);
	}
	
	public int updateParentLeaf(String pid) throws Exception {
		if(!pid.equals("0")) {//0为最根层节点，该节点为虚拟节点
			return this.getSqlSession().update(
					"com.czp.menu.mapper.IMenuMapper.updateParentLeaf", pid);
		}
		return 0;
	}
	
	public int modifyMenuNode(Menu menu) throws Exception {
		if(menu.getEnableStatus().equals("0")) {
			//当节点禁用时将颜色变为红色
			menu.setCls("c-tree-node-red");
		}
		return this.getSqlSession().update(
				"com.czp.menu.mapper.IMenuMapper.modifyMenuNode", menu);
	}
	
	public int deleteMenuNode(Menu menu) throws Exception {
		return this.getSqlSession().update(
				"com.czp.menu.mapper.IMenuMapper.deleteMenuNode", menu);
	}
	
	public String getMenuMaxOrderByPid(Menu menu) throws Exception {
		if(menu.getPid().equals("")) {
			menu.setPid("0");
		}
		return this.getSqlSession().selectOne(
				"com.czp.menu.mapper.IMenuMapper.getMenuMaxOrderByPid", menu);
	}
	
	public int sortMenuNode(Menu menu) throws Exception {
		return this.getSqlSession().update(
				"com.czp.menu.mapper.IMenuMapper.sortMenuNode", menu);
	}
	
	public List<Icon> queryMenuIcon(Icon icon, RowBounds bounds) throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.queryMenuIcon", icon, bounds);
	}
	
	public int queryIconCount() throws Exception {
		return this.getSqlSession().selectOne(
				"com.czp.menu.mapper.IMenuMapper.queryIconCount");
	}
	
	public List<Menu> getMenuRootList() throws Exception {
		return this.getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.getMenuRootList");
	}
	
	public List<Menu> getMenuLeafByPid(String pid) throws Exception {
		return getSqlSession().selectList(
				"com.czp.menu.mapper.IMenuMapper.getMenuLeafById", pid);
	}
}
